# Initial code only makes assessment for NQ quantifier, need to add code for TL, NC, and OT quantifiers
import Parser, Post, FileIO
from NotAQuestionQuantifier import questionMarkPresent, _5W1H_Present, regexpMatch

from Resources.config import NOT_A_AQUESTION_QUANTIFIER_SPLIT

fileStream = FileIO.openTrainingFile(FileIO.READ_MODE) 
Parser.readHeader(fileStream)

withQM = 0
withoutQM = 0

with_5W1H = 0
without_5W1H = 0

matched_regexp = 0
unmatched_regexp = 0

overall_quantified_val_acc = 0
counter = 0

open_quantified_val_acc = 0
open_counter = 0

closed_quantified_val_acc = 0
closed_counter = 0

# NQ = Not a question
NQ_quantified_val_acc = 0
NQ_counter = 0

while (not Parser.isEndOfFile(fileStream)):
	
	quantified_val = 0
	newPost = Parser.readPost(fileStream)
	
	bodyMarkdown = newPost.bodyMarkdown

	QM = questionMarkPresent(bodyMarkdown)
	_5w1H_match = _5W1H_Present(bodyMarkdown);
	regexp_match = regexpMatch(bodyMarkdown)

	if QM:
		withQM += 1
		quantified_val += NOT_A_AQUESTION_QUANTIFIER_SPLIT['QUESTION_MARK_PRESENT']
	else:
		withoutQM += 1

	if _5w1H_match:
		with_5W1H += 1
		quantified_val += NOT_A_AQUESTION_QUANTIFIER_SPLIT['5W1H_PRESENT']
	else:
		without_5W1H += 1

	if regexp_match:
		matched_regexp += 1
		quantified_val += NOT_A_AQUESTION_QUANTIFIER_SPLIT['REGEXP_MATCH']
	else:
		unmatched_regexp += 1

	overall_quantified_val_acc += quantified_val
	counter += 1

	post_status = Post.getFullPostStatus( newPost.openStatus )

	if ( post_status == "open"):
		open_quantified_val_acc += quantified_val
		open_counter += 1
	else:
		closed_quantified_val_acc += quantified_val
		closed_counter += 1

	if ( post_status == "not a real question"):
		NQ_quantified_val_acc += quantified_val
		NQ_counter += 1 

	print "Post - ", counter, "; QM - ", QM, "; 5W1H - ", _5w1H_match, "; regexp match - ", regexp_match, "; Post status - \'" + post_status + "\'"

import datetime
f = open("Output/quantifiers_assessment_" + datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + ".log", 'w')

f.write( "All posts: " + str(counter) )
f.write( "\n\nPosts with question mark:    " + str(withQM) )
f.write( "\nPosts without question mark: " + str(withoutQM) )
f.write( "\n\nPosts with 5W1H:    " + str(with_5W1H) )
f.write( "\nPosts without 5W1H: " + str(without_5W1H) )
f.write( "\n\nPosts matched question regexp:   " + str(matched_regexp) )
f.write( "\nPosts unmatched question regexp: " + str(unmatched_regexp) )
f.write( "\n\nAverage NQ quantifier value over all posts:    " + str(float(overall_quantified_val_acc)/counter) )
f.write( "\nAverage NQ quantifier value over open posts:   " + str(float(open_quantified_val_acc)/open_counter) )
f.write( "\nAverage NQ quantifier value over closed posts: " + str(float(closed_quantified_val_acc)/closed_counter) )
f.write( "\nAverage NQ quantifier value over NQ posts:     " + str(float(NQ_quantified_val_acc)/NQ_counter) )

f.close()